import { _decorator, Component, Node, tween, Tween, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Tools')
export class Tools {
    static getDistance(x1, y1, x2, y2) {
        return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }

    /**
     * 判断碰撞
     */
    static CheckHit(_node0: Node, _node1: Node): boolean {

        if (Math.abs(_node0.getWorldPosition().x - _node1.getWorldPosition().x) < _node1.getComponent(UITransform).width / 2 &&
            Math.abs(_node0.getWorldPosition().y - _node1.getWorldPosition().y) < _node1.getComponent(UITransform).height / 2) {
            return true;
        }

        return false;

    }

    /**
    * 小丑牌加分效果
    */
    static JokerEffect(_node: Node, _time: number = 0.1, _cb: Function = null) {
        Tween.stopAllByTarget(_node);
        _node.scale = new Vec3(1, 1, 1);
        let scale_num: number = 0.9;
        let scale_num1: number = 1.1;
        tween(_node).
            to(0.05, { scale: new Vec3(scale_num, scale_num, scale_num) }).
            to(0.05, { scale: new Vec3(1, 1, 1) }).
            to(0.05, { scale: new Vec3(scale_num1, scale_num1, scale_num1) }).
            to(0.05, { scale: new Vec3(1, 1, 1) }).
            call(() => { _cb && _cb() }).start();
    }

    /**
    * 小丑牌加分效果
    */
    static JokerEffect1(_node: Node, _time: number = 0.1, _cb: Function = null) {
        Tween.stopAllByTarget(_node);
        _node.angle = 0;
        let change_angle: number = 7;
        tween(_node).
            to(0.02, { angle: change_angle }).
            to(0.02, { angle: 0 }).
            to(0.02, { angle: -change_angle }).
            to(0.02, { angle: 0 }).
            to(0.02, { angle: change_angle }).
            to(0.02, { angle: 0 }).
            to(0.02, { angle: -change_angle }).
            to(0.02, { angle: 0 }).
            call(() => { _cb && _cb() }).start();
    }

}


